<?php
require __DIR__ . '/../bootstrap.php';
require_once __DIR__ . "/../auth.php";
require_once __DIR__ . "/loadUserListSimHelper.class.php";
$em = require __DIR__ . '/../example/em.php';
$auth = new \Auth\Auth($em);

// Данные которые передаются на вход
//$_POST['dateBegin'];
//$_POST['dateEnd'];

//Данные которые мне нужны

//$_GET['dateBegin']='15.06.2013';
//$_GET['dateEnd']='16.07.2013';

$dateBegin = convertDate($_GET['dateBegin']);
$dateEnd = convertDate($_GET['dateEnd']);

/*
echo '<pre>';
print_r($dateBegin);
echo '<br>';
print_r($dateEnd);
echo '</pre>';
*/


$qb = $em->createQueryBuilder();
$qb->select(array("h.date", "u.name as userName", "g.name as groupName", "h.ip"))
    ->from("Entity\UserHistory", "h")
    ->join("h.user", "u")
    ->join("u.group", "g")
    ->where($qb->expr()->between("h.date", ":dateBegin", ":dateEnd"))
    ->setParameter("dateBegin", $dateBegin . ' 00:00:00')
    ->setParameter("dateEnd", $dateEnd . ' 23:59:59');
$rows = $qb->getQuery()->getResult();

/*
echo '<pre>';
print_r($rows);
echo '</pre>';
*/
$result = array();

$count = 0;
array_map(function ($row) use (&$result, &$count) {
    $result["aaData"][] = array(
        ++$count,
        $row['date']->format("d.m.Y"),
        $row["userName"],
        $row["groupName"],
        $row["ip"],
    );
}, $rows);

echo json_encode($result);

exit();

echo '{
  "aaData": [
    [
      "1",
      "10.05.2013",
      "Admin",
      "Смартсим",
      "192.168.1.1"
    ],
    [
      "2",
      "15.05.2013",
      "Агент",
      "Агент",
      "192.168.1.1"
    ],
    [
      "3",
      "12.05.2013",
      "Субагент",
      "Субагент",
      "192.168.1.1"
    ]
  ]
}';


?>
